package com.future;

/**
 * Description: NC22 合并两个有序的数组 ( 88. 合并两个有序数组)
 *
 * @author weiruibai.vendor
 * Date: 2022/7/27 09:26
 */
public class Solution_NC22 {

    public static void main(String[] args) {
        int[] nums1 = {1, 2, 3, 0, 0, 0};
        int m = 3;
        int[] nums2 = {2, 5, 6};
        int n = 3;
        // =========
        /*nums1 = new int[]{0};
        m = 0;
        nums2 = new int[]{1};
        n = 1;*/
        merge(nums1, m, nums2, n);
    }

    public static void merge(int A[], int m, int B[], int n) {
        if (n == 0) {
            return;
        }
        int N = m + n - 1;
        int aL = m - 1;
        int bL = n - 1;
        while (aL >= 0 || bL >= 0) {
            int aV = aL >= 0 ? A[aL] : Integer.MIN_VALUE;
            int bV = bL >= 0 ? B[bL] : Integer.MIN_VALUE;
            if (aV >= bV) {
                A[N--] = aV;
                aL--;
            } else {
                A[N--] = bV;
                bL--;
            }
        }
    }
}
